package com.ptszyxx.popose.module.main.mine.vm;

import android.app.Application;

import androidx.annotation.NonNull;

import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.enums.MineFollowTabEnum;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseListResponse;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessListResult;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.http.data.entity.mine.FollowEntity;
import com.ysg.http.data.entity.mine.IntimacyEntity;
import com.ysg.utils.YSPUtils;
import com.ysg.utils.YToastUtil;

import java.util.HashMap;
import java.util.Map;

public class MineFollowTabVM extends BaseViewModel<CommonRepository> {
    public MineFollowTabEnum tabEnum;
    public FollowEntity followEntity;
    public int position;

    public MineFollowTabVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onRefreshEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onRefreshItemEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onRefreshIntimacyEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onCloseCommand = new BindingCommand(() ->
        YActivityUtil.getInstance().close(this)
    );
    public BindingCommand onRefreshCommand = new BindingCommand(() ->
            request(true)
    );
    public BindingCommand onLoadMoreCommand = new BindingCommand(() ->
            request(false)
    );
    //=========================================接口：start===========================================

    public void request(boolean refresh){
            switch (tabEnum){
                case intimacy:
                    requestIntimacyList();
                    break;
                case follow:
                    requestFollowList(refresh);
                    break;
                case fans:
                    requestFansList(refresh);
                    break;
            }
    }

    /**
     * 关注列表
     */
    public void requestFollowList(boolean refresh){
        if (refresh){
            page = 1;
        } else {
            page++;
        }
        Map<String, String> map = new HashMap<>();
        map.put("pageno", page + "");
        HttpUtils.getInstance()
                .list(model.followList(map), this,
                        new OnSuccessListResult<FollowEntity>() {
                            @Override
                            public void onSuccessResult(BaseListResponse<FollowEntity> response) {
                                uc.onRefreshEvent.setValue(response.getData());
                            }
                        });
    }

    /**
     * 粉丝列表
     */
    public void requestFansList(boolean refresh){
        if (refresh){
            page = 1;
        } else {
            page++;
        }
        Map<String, String> map = new HashMap<>();
        map.put("pageno", page + "");
        HttpUtils.getInstance()
                .list(model.fansList(map), this,
                        new OnSuccessListResult<FollowEntity>() {
                            @Override
                            public void onSuccessResult(BaseListResponse<FollowEntity> response) {
                                uc.onRefreshEvent.setValue(response.getData());
                            }
                        });
    }

    /**
     * 亲密度列表
     */
    public void requestIntimacyList(){
        Map<String, String> map = new HashMap<>();
        map.put("beiUserId", YSPUtils.getInstance().getUserId());
        HttpUtils.getInstance()
                .list(model.intimacyList(map), this,
                        new OnSuccessListResult<IntimacyEntity>() {
                            @Override
                            public void onSuccessResult(BaseListResponse<IntimacyEntity> response) {
                                uc.onRefreshIntimacyEvent.setValue(response.getData());
                            }
                        });
    }

    /**
     * 关注和取消关注
     */
    public void onFollowClick(FollowEntity entity, int position){
        if (entity == null){
            return;
        }
        this.followEntity = entity;
        this.position = position;
        Map<String, String> map = new HashMap<>();
        map.put("userid", entity.getUserid() + "");
        map.put("leixing", entity.isGuanzhu()? "2": "1");

        HttpUtils.getInstance()
                .data(model.follow(map), this,
                        new OnSuccessResult() {
                            @Override
                            public void onSuccessResult(BaseResponse response) {
                                YToastUtil.showShort(R.string.success);
                                uc.onRefreshItemEvent.call();
                            }
                        });
    }


}
